<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $catalogue = $_POST['catalogue'];
  $name = addslashes($_POST['name']);
  
  $sql = "INSERT INTO product_set VALUES('','$catalogue','$name','')";

  $db->execute($sql);
  
  if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
    $product_set_id = $db->lastInsertedId();
    
    
    //
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_set_id, $_FILES['img']['tmp_name']);
    //$path = "../../images/products/set/".$product_set_id;
    //mkdir($path);
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(350,260);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE product_set SET product_set_img ='$name' WHERE product_set_id=$product_set_id";
    //$db->execute($sql);
    
    $handle = new Upload($_FILES['img']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 350;
      $handle->image_y = 260;
      $handle->Process('../../images/products/set/'.$product_set_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE product_set SET product_set_img ='".$handle->file_dst_name."' WHERE product_set_id=$product_set_id";
         if(!mysql_query($sql))
         {
            @unlink('../../images/products/set/'.$product_set_id.'/'.$handle->file_dst_name);
         }
      }
      $handle->clean();
    }
  }
  
  header("Location:index.php");
?>